package org.example.maven.service.Impl;

import com.github.pagehelper.PageHelper;
import org.example.maven.mapper.RoleMapper;
import org.example.maven.pojo.role.AddRole;
import org.example.maven.pojo.role.RoleListTable;
import org.example.maven.pojo.role.SearchRole;
import org.example.maven.pojo.utils.Select;
import org.example.maven.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<RoleListTable> getAllRoleList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return roleMapper.getAllRoleList();
    }

    @Override
    public List<RoleListTable> getSpecieRole(SearchRole searchRole,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return roleMapper.getSpecieRole(searchRole);
    }

    @Override
    public AddRole getEditRole(Integer id) {
        return roleMapper.getEditRole(id);
    }

    @Override
    public String getRoleName(String roleName) {
        return roleMapper.getRoleName(roleName);
    }

    @Override
    public void addRole(AddRole addRole) {
        roleMapper.addRole(addRole);
    }

    @Override
    public void addRolePermission(String id, String[] permission) {
        roleMapper.addRolePermission(id,permission);
    }

    @Override
    public Integer getRoleCount(SearchRole searchRole) {
        return roleMapper.getRoleCount(searchRole);
    }

    @Override
    public void deleteRole(String[] id) {
        roleMapper.deleteRoleById(id);
    }

    @Override
    public void updateRoleById(AddRole addRole) {
        roleMapper.updateRoleById(addRole);
    }

    @Override
    public String[] getRolePermission(Integer id) {
        return roleMapper.getRolePermission(id);
    }

    @Override
    public void deleteRolePermission(Integer id) {
        roleMapper.deleteRolePermission(id);
    }

    @Override
    public List<Map<String, String>> getRoleSelect() {
        List<Select> list=roleMapper.getRoleSelect();
        List<Map<String,String>> result=new ArrayList<>();
        for (Select s : list) {
            Map<String, String> map = new HashMap<>(); // 每次循环创建新 map
            map.put("value", s.getValue());
            map.put("label", s.getLabel());
            result.add(map); // 直接添加新条目
        }
        return result;
    }
}
